#include "constants.hpp"
#include "hash.hpp"
#include <cstring>
#include <cstdio>

hash::hash() {
	harr=new char*[HASHSIZE];
	hind=new int[HASHSIZE];
	hobr=new int[HASHSIZE];
	for (int i=0;i<HASHSIZE;i++) harr[i]=0;
	maxn=0;
}

int hash::get(char *s) {
	long long hashnum=0;
	for (int i=0;s[i];i++) {
		hashnum=(hashnum*HASH_MULT+s[i])%HASHSIZE;
	}
	while (harr[hashnum]!=0 && strcmp(harr[hashnum],s)) hashnum=(hashnum+1)%HASHSIZE;
	if (harr[hashnum]==0) {
		harr[hashnum]=s;
		hobr[maxn]=hashnum;
		hind[hashnum]=maxn++;
	}
	return hind[hashnum];
}

char *hash::str(int i) {
	return harr[hobr[i]];
}
